package com.ta.gulimall.product.service.impl;

import com.ta.gulimall.product.view.entity.BrandEntity;
import com.ta.gulimall.product.view.entity.CategoryEntity;
import com.ta.gulimall.product.service.BrandService;
import com.ta.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ta.gulimall.product.view.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.Query;

import com.ta.gulimall.product.dao.CategoryBrandRelationDao;
import com.ta.gulimall.product.view.entity.CategoryBrandRelationEntity;
import com.ta.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    CategoryService categoryService;
    @Autowired
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        CategoryEntity category = categoryService.getById(categoryBrandRelation.getCatelogId());
        BrandEntity brand = brandService.getById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateCascadeBrand(BrandEntity brand) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> queryWrapper = new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getBrandId, brand.getBrandId());
        update(new CategoryBrandRelationEntity().setBrandName(brand.getName()), queryWrapper);
    }

    @Override
    public void updateCascadeCategory(CategoryEntity category) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> queryWrapper = new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, category.getCatId());
        update(new CategoryBrandRelationEntity().setCatelogName(category.getName()), queryWrapper);
    }

    @Override
    public List<CategoryBrandRelationEntity> getCategoryBrandRelations(Long brandId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> queryWrapper = new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getBrandId, brandId);
        return this.list(queryWrapper);
    }

    @Override
    public List<BrandVo> getBrandVos(Long catId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId);
        List<CategoryBrandRelationEntity> categoryBrandRelations = Optional.ofNullable(this.list(wrapper)).orElse(new ArrayList<>());

        return categoryBrandRelations.stream().map(this::getBrandVo).collect(Collectors.toList());
    }

    private BrandVo getBrandVo(CategoryBrandRelationEntity categoryBrandRelation) {
        return new BrandVo().setBrandId(categoryBrandRelation.getBrandId()).setBrandName(categoryBrandRelation.getBrandName());
    }

}